package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.CompanySearchItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class CompanySearchThread extends BasicThread {

	public interface CompanySearchThreadCallback {
		public void OnCompanySearchCompleted(boolean isNetworkConntected
				, ArrayList<CompanySearchItem> arrCompanySearchItem
				, String message);
		public void OnCompanySearchError(ErrorItem commpanyItem);
	}

	private String url;
	private CompanySearchThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String cprNm;
	
	public CompanySearchThread(Context context,
			CompanySearchThreadCallback callback, String cprNm, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.cprNm = cprNm;
	}

	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.SEARCHCPRNM, cprNm);
		String param = "";
		param = parameterReturn(Const.REQ026, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnCompanySearchCompleted(false, null, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnCompanySearchError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			ArrayList<CompanySearchItem> arrCompanySearchItem = new ArrayList<CompanySearchItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				callback.OnCompanySearchError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				CompanySearchItem companySearchItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject prod = arrayLogin.getJSONObject(i);
					
					companySearchItem = new CompanySearchItem();
					companySearchItem.setCprNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, CompanySearchItem.CPRNM)));
					companySearchItem.setCeoNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, CompanySearchItem.CEONM)));
					companySearchItem.setVenCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, CompanySearchItem.VENCD)));
					
					arrCompanySearchItem.add(companySearchItem);
				}
				callback.OnCompanySearchCompleted(true, arrCompanySearchItem, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getCompanySearchParams(cprNm);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);*/
		

		/*try {		
			
		} catch (JSONException e) {
		}*/
	}
}
